#ifndef __GAME_CRON_H__
#define __GAME_CRON_H__

#include "game_def.h"
#include "game_ev.h"
#include <functional>
#include <map>
#include <string.h>
#include <string>

class CGameCron{
private:
  event_base* _evbase;
  event* _event;
  struct Cron_t
  {
    UINT64 _last;
    std::string _cronexpr;
    std::function<void()> _func;
  };

  std::map<std::string,Cron_t*> _map;

  static void __timer_cb(int sock, short which, void *arg) ;

  void tick();
  void onTick();
public:
  BOOL init();
  BOOL init(event_base*);
  void cron(const std::function<void()>& fn,const std::string& cronexpr);
  void cron(const std::function<void()>& fn,const std::string& cronexpr,const std::string& key);
  void uncron(const std::string& key);

  static BOOL next(UINT64* dest,const char* cronexpr);
  static BOOL next(UINT64* dest,UINT64 now,const char* cronexpr);

};

extern CGameCron g_CGameCron;

#endif
